package com.ruoyi.material.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.material.PurchaseConstants;
import com.ruoyi.material.domain.WarehouseOutProduct;
import com.ruoyi.material.domain.WarehouseStock;
import com.ruoyi.material.domain.WarehouseStockRecord;
import com.ruoyi.material.mapper.WarehouseOutProductMapper;
import com.ruoyi.material.mapper.WarehouseStockMapper;
import com.ruoyi.material.mapper.WarehouseStockRecordMapper;
import com.ruoyi.material.service.IWarehouseOutProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 出库单商品关联Service业务层处理
 *
 * @author wng
 * @date 2024-08-04
 */
@Service
public class WarehouseOutProductServiceImpl implements IWarehouseOutProductService {
    @Resource
    private WarehouseOutProductMapper warehouseOutProductMapper;
    @Resource
    private WarehouseStockMapper warehouseStockMapper;
    @Resource
    private WarehouseStockRecordMapper warehouseStockRecordMapper;

    /**
     * 查询出库单商品关联
     *
     * @param id 出库单商品关联主键
     * @return 出库单商品关联
     */
    @Override
    public WarehouseOutProduct selectWarehouseOutProductById(Long id) {
        return warehouseOutProductMapper.selectWarehouseOutProductById(id);
    }

    /**
     * 查询出库单商品关联列表
     *
     * @param warehouseOutProduct 出库单商品关联
     * @return 出库单商品关联
     */
    @Override
    public List<WarehouseOutProduct> selectWarehouseOutProductList(WarehouseOutProduct warehouseOutProduct) {
        return warehouseOutProductMapper.selectWarehouseOutProductList(warehouseOutProduct);
    }

    /**
     * 新增出库单商品关联
     *
     * @param warehouseOutProduct 出库单商品关联
     * @return 结果
     */
    @Override
    public int insertWarehouseOutProduct(WarehouseOutProduct warehouseOutProduct) {
        return warehouseOutProductMapper.insertWarehouseOutProduct(warehouseOutProduct);
    }

    /**
     * 修改出库单商品关联
     *
     * @param warehouseOutProduct 出库单商品关联
     * @return 结果
     */
    @Override
    public int updateWarehouseOutProduct(WarehouseOutProduct warehouseOutProduct) {
        return warehouseOutProductMapper.updateWarehouseOutProduct(warehouseOutProduct);
    }

    /**
     * 批量删除出库单商品关联
     *
     * @param ids 需要删除的出库单商品关联主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteWarehouseOutProductByIds(Long[] ids) {
        //库存加回去
        for (Long id : ids) {
            WarehouseOutProduct warehouseOutProduct = warehouseOutProductMapper.selectWarehouseOutProductById(id);
            WarehouseStock warehouseStock = warehouseStockMapper.selectWarehouseStockById(warehouseOutProduct.getStockId());

            BigDecimal quantity_before = warehouseStock.getQuantity();

            warehouseStock.setQuantity(warehouseStock.getQuantity().add(warehouseOutProduct.getQuantity()));
            warehouseStockMapper.updateWarehouseStock(warehouseStock);

            //新增库存变动记录
            WarehouseStockRecord stockRecord = getStockRecord(warehouseOutProduct, quantity_before, warehouseStock, PurchaseConstants.StockTypeEnum.Out_del);
            warehouseStockRecordMapper.insertWarehouseStockRecord(stockRecord);
        }
        int i = warehouseOutProductMapper.deleteWarehouseOutProductByIds(ids);
        return i;
    }

    private static WarehouseStockRecord getStockRecord(WarehouseOutProduct item, BigDecimal quantity_before, WarehouseStock stock, PurchaseConstants.StockTypeEnum stockTypeEnum) {
        WarehouseStockRecord stockRecord = new WarehouseStockRecord();
        stockRecord.setType(stockTypeEnum);
        stockRecord.setName(item.getName());

        stockRecord.setStockBefore(quantity_before);
        stockRecord.setStockAfter(stock.getQuantity());
        stockRecord.setUnit(item.getUnit());
        stockRecord.setfType(item.getfType());
        stockRecord.setfTypeSub(item.getfTypeSub());
        stockRecord.setWhetherImport(item.getWhetherImport());
        stockRecord.setCreateTime(new Date());

        stockRecord.setQuantity(stockRecord.getStockBefore().subtract(stockRecord.getStockAfter()).abs());
        return stockRecord;
    }

    /**
     * 删除出库单商品关联信息
     *
     * @param id 出库单商品关联主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseOutProductById(Long id) {
        return warehouseOutProductMapper.deleteWarehouseOutProductById(id);
    }
}
